//
//  CommentViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/1/11.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//  纯代码实现底部输入框

#import "CommentViewController.h"
#import "CommentInputView.h"
#import "Masonry.h"
#import "BottomCommentViewController.h"

@interface CommentViewController () <CommentInputViewDelegate>

@property (nonatomic, strong) CommentInputView *commentInputView;

@end

@implementation CommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.navigationItem.title = @"纯代码实现底部输入框";
    self.view.backgroundColor = [UIColor orangeColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)setupUI
{
    [self.view addSubview:self.commentInputView];
    /// 添加输入框约束
    [self addCommentInputViewMasonry];
}

- (void)addCommentInputViewMasonry
{
    [self.commentInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-BottomHeight);
        make.height.mas_greaterThanOrEqualTo(49);
    }];
    [self.view layoutIfNeeded];
}

- (void)keyboardWillShow:(NSNotification *)notification
{}

- (void)keyboardDidShow:(NSNotification *)notification
{}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [self.commentInputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-BottomHeight);
    }];
    [self.view layoutIfNeeded];
}

- (void)keyboardFrameWillChange:(NSNotification *)notification
{
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [self.commentInputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).mas_offset(-keyboardFrame.size.height);
    }];
    [self.view layoutIfNeeded];
}

#pragma mark --- <CommentInputViewDelegate>
- (void)commentInputView:(CommentInputView *)commentInputView
 didChangeTextViewHeight:(CGFloat)textViewHeight
{
    
}

- (void)commentInputView:(CommentInputView *)commentInputView
          sendTextString:(NSString *)sendTextString
{
    
}

#pragma mark --- <Lazy loading>
- (CommentInputView *)commentInputView
{
    if (_commentInputView == nil) {
        _commentInputView = [[CommentInputView alloc] init];
        _commentInputView.delegate = self;
    }
    return _commentInputView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    BottomCommentViewController *vc = [[BottomCommentViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
//    [self.navigationController pushViewController:vc animated:YES];
}

@end
